const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet, HttpPost, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/tablet/aged')
class AgedCtrl extends Controller {

    //小区老人列表
    @HttpGet('/list')
    @ExceptionHandler()
    async getAgedList() {
        const rule = {
            pn: 'int',
            ps: 'int'
        };
        this.validate(rule, this.ctx.request.query);
        this.result.data = await this.service.tablet.aged.main.getAgedList(this.ctx.request.query.pn, this.ctx.request.query.ps);
        return '查询成功'
    }

    //获取老人信息
    @HttpGet('/:id')
    @ExceptionHandler()
    async getAged() {
        const rule = {
            id: 'int'
        };
        this.validate(rule, this.ctx.params);
        this.result.data = await this.service.tablet.aged.main.getAged(this.ctx.params.id);
        return '查询成功'
    }

    //批量提交老人信息
    @HttpPost('/import')
    @ExceptionHandler()
    async import() {
        const arrRule = {
            aged_list: {
                type: 'array',
                itemType: 'object',
                rule: {
                    name: 'string',
                    sex: 'int',
                    phone: 'string',
                    birth: 'int',
                    id_card_num: 'string',
                    aged_pic: 'string',
                    citizen_card_pic: 'string',
                    subdistrict_id: 'int',
                    house_num: 'string'
                }
            }
        };
        this.validate(arrRule, this.ctx.request.body);
        await this.service.tablet.aged.main.import(this.ctx.request.body.aged_list);
        return '提交成功'
    }
}

module.exports = AgedCtrl;
